<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        活动添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/css/main.css" media="all">
    <script type="text/javascript" src="/lib/loading/okLoading.js"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div id="app">
<div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>活动管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <el-form :model="activity" :rules="rules" ref="activityFormRef" label-width="120px" class="demo-ruleForm">
        <el-form-item label="所属活动" prop="communityName">
            <el-input v-model="activity.communityName"></el-input>
        </el-form-item>
        <el-form-item label="活动标题" prop="title">
            <el-input v-model="activity.title"></el-input>
        </el-form-item>
        <el-form-item label="活动地点" prop="address">
            <el-input v-model="activity.address"></el-input>
        </el-form-item>
        <el-form-item label="举办单位" prop="organizer">
            <el-input v-model="activity.organizer"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
            <el-input v-model="activity.startTime"></el-input>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
            <el-input v-model="activity.endTime"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-radio-group v-model="activity.status">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">停用</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
            <el-button @click="clearAll()" style="text-align: center">重置</el-button>
        </el-form-item>
    </el-form>
</div>
</div>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script src="js/x-layui.js" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script src="/js/vue.js" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script src="/js/index.js"></script>
<!--导入Axios依赖包-->
<script src="/js/axios.js" charset="utf-8"></script>
<script type="text/javascript">
    var app = new Vue({
        el:"#app",
        data:{
            title:"添加活动",
            activity:{thumbnail:"",id:""},
            fileList:[],
            rules: {
                communityName: [
                    { required: true, message: '请输入小区名称', trigger: 'blur' },
                    { max: 10, message: '长度小于10个字符', trigger: 'blur' }
                ],
                title: [
                    { required: true, message: '请输入活动标题', trigger: 'blur' },
                    { min: 5,max: 20, message: '长度必须大于5、小于20个字符', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '请输入活动地址', trigger: 'blur' }
                ],
                organizer: [
                    { required: true, message: '请输入举办单位名称', trigger: 'blur' }
                ],
                startTime: [
                    { required: true, message: '请输入开始时间', trigger: 'blur' }
                ],
                endTime: [
                    { required: true, message: '请输入结束时间', trigger: 'blur' },
                ],
                status:[
                    { required: true, message: '请输入状态', trigger: 'blur' }
                ]
            }
        },
        methods:{
            findById(){
                axios.get("/activity/findById?id="+this.activity.id).then((res)=>{
                    this.activity = res.data.data;
                    this.fileList.push({url:"http://localhost/estate"+this.activity.thumbnail});
                });
            },
            handleSuccess(response, file, fileList){
                this.activity.thumbnail=response.data;
            },
            handleRemove() {
                axios.get("/estate/delfile?fileName="+this.activity.thumbnail).then((res)=>{
                    app.activity.thumbnail="";
                    if(res.data.flag){
                        app.fileList=[];
                        this.$message({
                            message: '图片删除成功',
                            type: 'success'
                        });
                    }
                });
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            //文件删除提示
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
            //表达重置
            clearAll() {
                this.$refs.activityFormRef.resetFields();
                if(this.fileList.length>0) {
                    this.handleRemove();
                }
            },
            save(){
                var method = "";
                if(this.activity.id != null){
                    method = "update"
                }else{
                    method = "add"
                }
                axios.post("/activity/"+method,this.activity).then((res)=>{
                    if(res.data.flag){
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            imgUrl(){
                return "http://localhost/estate"+app.activity.thumbnail;
            },
            getQueryString(name){
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        },
        created:function(){
            layui.use(['element','layer','form'], function(){
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，活动ID
            this.activity.id = this.getQueryString("id");
            //根据活动ID获取活动对象，进行数据回显
            if(this.activity.id != null && this.activity.id != ''){
                this.title="修改活动";
                this.findById();
            }
        }
    });
</script>
</body>
</html>